package labox.innovation.gameserver.network.serverpackets;

import javolution.util.FastMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.datatables.ShopItemsListConfig;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.ShopItem;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.skills.funcs.AttributeStringSplice;
import labox.innovation.gameserver.templates.item.L2Item;

/**
 * ������ƷЭ��
 * @author zjj
 *
 */
public final class ItemList extends L2GameServerPacket {
	private static final Logger _log = LoggerFactory.getLogger(ItemList.class.getName());

	private static final String _S__11_ITEMLIST = "[S] 0x70 ItemList";

	private L2ItemInstance[] _items;
	
	private FuzePcInstance _pc;

	private int _maxInventoryNums;

	private int _equipitems; //��װ���������ϵ�װ����

	public ItemList(FuzePcInstance cha, boolean showWindow) {
		_items = cha.getInventory().getItems();
		_equipitems = cha.getInventory().getEquipItems();
		_maxInventoryNums = cha.getInventory().getInventoryLimit();
		_pc = cha;
		if (_log.isDebugEnabled()) {
			showDebug();
		}
	}

	public ItemList(L2ItemInstance[] items, boolean showWindow) {
		_items = items;
		if (_log.isDebugEnabled()) {
			showDebug();
		}
	}

	private void showDebug() {
		_log.debug("��Ʒ����:" + _items.length );
		for (L2ItemInstance temp : _items) {
			_log.debug("item:" + temp.getItem().getName() + " isEquip:" + temp.isEquipped() + " ���õ�λ��" + temp.getLocationSlot() + "��Ʒ����:" + temp.getCount() + "��Ʒuid:" + temp.getObjectId() + "��Ʒӵ����id��" + temp.getOwnerId() + "��Ʒģ��id:" + temp.getItemId() );
		}
	}

	@Override
	protected final void writeImpl() {
		writeByte(PTL_ITEMLIST);
		writeByte(_maxInventoryNums);

		int count = _items.length;
		writeByte(count);
		writeByte(_equipitems); //��װ���ڽ�ɫ���ϵ�װ����

		for (L2ItemInstance temp : _items) {
			if (temp == null || temp.getItem() == null)
				continue;

			writeLong(temp.getObjectId());
			writeString(temp.getName());    //������Ʒ�� �ֶ�  ��Ҫ����Э��
			writeInt(temp.getItemId());
			writeByte(temp.getLocationSlot());
			writeInt(temp.getCount());
			
			writeByte(temp.isEquipped() ? 0x01 : 0x00);
			writeByte(temp.getLocationSlot());//temp.getItem().getBodyPart() edit by squll 2010.4.30

			writeInt(temp.isTimeLimitedItem() ? (int) (temp.getRemainingTime() / 1000) : -1);
			writeByte(temp.getItem().getItemIconId());
			//������ɫ �ֶΡ�  �ݲ���
//			writeInt( AttributeStringSplice.getInstance().getColor(temp.getItemGradeType() ));
//			writeShort( temp.getItemMask() );
		}
		
		//�·���ɫ�������
		writeInt(_pc.getMaxHp());
		writeInt(_pc.getMaxMp());
		writeInt(_pc.getMinPAtk());
		writeInt(_pc.getMaxPAtk());
		writeInt(_pc.getMinMAtk());
		writeInt(_pc.getMaxMAtk());
		writeInt(_pc.getPDef());
		writeInt(_pc.getMDef());
		//�������ֵ
		//ħ�����ֵ
		//���?��
		//ħ������
		//�������
		//ħ������
		
		
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.serverpackets.ServerBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _S__11_ITEMLIST;
	}
}
